// LockGuard.hpp
#pragma once

#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t* pmtx)
        : _pmtx(pmtx)
    {}

    void lock()
    {
        std::cout << "进行加锁" << std::endl;
        pthread_mutex_lock(_pmtx);
    }

    void unlock()
    {
        std::cout << "进行解锁" << std::endl;
        pthread_mutex_unlock(_pmtx);
    }

    ~Mutex()
    {}

private:
    pthread_mutex_t* _pmtx;
};

// RAII的加锁方式
class LockGuard
{
public:
    LockGuard(pthread_mutex_t* pmtx)
        : _mtx(pmtx)
    {
        _mtx.lock();
    }

    ~LockGuard()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};